package com.bilgeadam.javaweb.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilgeadam.javaweb.model.Authenticator;
import com.bilgeadam.javaweb.model.Dog;
import com.bilgeadam.javaweb.model.User;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 *
 * @author Bora SAYINER
 * @since Dec 27, 2014 4:01:07 PM
 * @version 1.0
 */
public class LoginServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// Dog dog = (Dog) getServletContext().getAttribute("dog");
		// System.out.println(dog.getName());
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		Authenticator authenticator = new Authenticator(getServletConfig().getInitParameter("username"), getServletConfig().getInitParameter("password"));
		String action = authenticator.authenticate(userName, password);
		if (action.equals("success")) {
			User user = new User(userName, password);
			req.getSession().setAttribute("dog", new Dog());
			req.setAttribute("user", user);
			req.getRequestDispatcher("home.jsp").forward(req, resp);
		} else {
			resp.sendRedirect("fail.jsp");
		}
	}
}
